package com.spring.sunflower.common

import android.app.Service
import android.os.Build
import android.os.VibrationEffect
import android.widget.TextView
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseCompatActivity
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.util.VibrateUtils
import com.spring.sunflower.widget.ToggleView
import com.google.common.base.Strings.repeat

import android.os.Vibrator


/**
 * Created by Kevin on 2021/9/27<br/>
 *
 * Describe:<br/>
 * 私信设置
 */
class PrivateChatSettingActivity : BaseCompatActivity() {
    private var toggleRing: ToggleView? = null
    private var toggleVibrate: ToggleView? = null
    private var tvRingDesc: TextView? = null
    private var tvVibrateDesc: TextView? = null
    override fun getLayoutID(): Int = R.layout.activity_private_chat_setting

    override fun initView() {
        tvTitle.text = "私信设置"
        val chatVibrate = getField(Constant.KEY_CHAT_VIBRATE, false)
        val chatRing = getField(Constant.KEY_CHAT_RING, true)
        toggleRing = findViewById(R.id.toggle_ring)
        toggleVibrate = findViewById(R.id.toggle_vibrate)
        tvRingDesc = findViewById(R.id.tv_ring_desc)
        tvVibrateDesc = findViewById(R.id.tv_vibrate_desc)
        toggleRing!!.setChecked(chatRing)
        toggleVibrate!!.setChecked(chatVibrate)
        toggleRing!!.setOnToggleClickListener(object : ToggleView.OnToggleClickListener {
            override fun onToggleClick(toggleState: Boolean) {
//                val pattern = longArrayOf(200, 1000)
//                val x = getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                    x.vibrate(VibrationEffect.createWaveform(pattern, -1))
//                } else {
//                    x.vibrate(pattern, 1)
//                }
                toggleRing!!.setChecked(toggleState)
                saveField(Constant.KEY_CHAT_RING, toggleState)
                if (toggleState) {
                    tvRingDesc!!.text = getString(R.string.title_chat_ring_open)
                } else {
                    tvRingDesc!!.text = getString(R.string.title_chat_ring_close)
                }
            }

        })
        toggleVibrate!!.setOnToggleClickListener(object : ToggleView.OnToggleClickListener {
            override fun onToggleClick(toggleState: Boolean) {
                toggleRing!!.setChecked(toggleState)
                saveField(Constant.KEY_CHAT_VIBRATE, toggleState)
                if (toggleState) {
                    VibrateUtils.getInstance(this@PrivateChatSettingActivity)
                        .playVibrateClick(VibrationEffect.EFFECT_TICK)
//                    VibrateUtils.getInstance(this@PrivateChatSettingActivity).playVibrate()
                    tvVibrateDesc!!.text = getString(R.string.title_chat_vibrate_open)
                } else {
                    tvVibrateDesc!!.text = getString(R.string.title_chat_vibrate_close)
                }
            }

        })
    }
}